(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.intindex.updateVerifyController', updateVerifyController);
	updateVerifyController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'intindexService' ];
	function updateVerifyController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, intindexService) {
		var ctrl = this, intindexInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.intIndex = {};
		ctrl.init = function() {
			var params = {
					intindexCode : intindexInfo.intindexCode
			};
			authService.updateVerify($scope.$stateParams.params).then(function(response) {
				intindexService.initOptions(ctrl);
				ctrl.intindex = response.data.model.newModel || {};
				ctrl.intindexOld = response.data.model.oldModel || {};
				ctrl.intindex.taskId = response.data.task.taskId;
			});
		};

		ctrl.updateVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.intindex.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.updateVerifyApprove(ctrl.intindex).then(approveSuccessful, approveFailed);
					}
				}
			});
		};

		ctrl.updateVerifyReject = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.intindex.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.updateVerifyReject(ctrl.intindex).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedSubCommpanyList = selectedRows;
		}

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.intindex.list',
					'state' : 'cda.intindex.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : [ 'error appeared: ' + angular.toJson(response.error) ]
			});
		}

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.intindex.list',
					'state' : 'cda.intindex.list',
					'params' : {}
				}]
			});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : [ 'error appeared: ' + angular.toJson(response.error) ]
			});
		}
	}
})(angular);